package cn.iocoder.yudao.module.water.controller.admin.prohis;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.prohis.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.prohis.ProHisDO;
import cn.iocoder.yudao.module.water.convert.prohis.ProHisConvert;
import cn.iocoder.yudao.module.water.service.prohis.ProHisService;

@Api(tags = "管理后台 - 生产历史")
@RestController
@RequestMapping("/water/pro-his")
@Validated
public class ProHisController {

    @Resource
    private ProHisService proHisService;

    @PostMapping("/create")
    @ApiOperation("创建生产历史")
    @PreAuthorize("@ss.hasPermission('water:pro-his:create')")
    public CommonResult<Long> createProHis(@Valid @RequestBody ProHisCreateReqVO createReqVO) {
        return success(proHisService.createProHis(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新生产历史")
    @PreAuthorize("@ss.hasPermission('water:pro-his:update')")
    public CommonResult<Boolean> updateProHis(@Valid @RequestBody ProHisUpdateReqVO updateReqVO) {
        proHisService.updateProHis(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除生产历史")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-his:delete')")
    public CommonResult<Boolean> deleteProHis(@RequestParam("id") Long id) {
        proHisService.deleteProHis(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得生产历史")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-his:query')")
    public CommonResult<ProHisRespVO> getProHis(@RequestParam("id") Long id) {
        ProHisDO proHis = proHisService.getProHis(id);
        return success(ProHisConvert.INSTANCE.convert(proHis));
    }

    @GetMapping("/list")
    @ApiOperation("获得生产历史列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:pro-his:query')")
    public CommonResult<List<ProHisRespVO>> getProHisList(@RequestParam("ids") Collection<Long> ids) {
        List<ProHisDO> list = proHisService.getProHisList(ids);
        return success(ProHisConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得生产历史分页")
    @PreAuthorize("@ss.hasPermission('water:pro-his:query')")
    public CommonResult<PageResult<ProHisRespVO>> getProHisPage(@Valid ProHisPageReqVO pageVO) {
        PageResult<ProHisDO> pageResult = proHisService.getProHisPage(pageVO);
        return success(ProHisConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出生产历史 Excel")
    @PreAuthorize("@ss.hasPermission('water:pro-his:export')")
    @OperateLog(type = EXPORT)
    public void exportProHisExcel(@Valid ProHisExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProHisDO> list = proHisService.getProHisList(exportReqVO);
        // 导出 Excel
        List<ProHisExcelVO> datas = ProHisConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生产历史.xls", "数据", ProHisExcelVO.class, datas);
    }

}
